<?php
namespace Admin\Controller\Patient;

use Admin\Model\Patient\DiseaseModel;
use Admin\Controller\AdminController;

class DiseaseController extends AdminController{
    /**
     * 疾病库列表
     * @author jiaolele
     */
    public function index()
    {
        //添加查询条件
        $name = I('get.disease_name');
        $section_office_id= I('get.section_office_id');
        $crowd_id=I('get.crowd_id');

        $where = array();
        if($name!==''){
            $where['disease_name'] = array('like','%'.$name.'%');
        }
        if($section_office_id){
            $where['section_office_id'] =$section_office_id;
        }
        if($crowd_id){
            $where['crowd_id'] =$crowd_id;
        }
        //查询列表信息
        $diseaseModel = new DiseaseModel();
        $list = $diseaseModel->getAdminList($where);
        //记录用户当前页面
        $this->getAuth();
        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        $keshi = M('DiseaseCategary')->field('id,type_name')->where(array('category'=>0))->select();//查询所有科室分类
        $renqun= M('DiseaseCategary')->field('id,type_name')->where(array('category'=>1))->select();//查询所有人群分类
        $this->assign('keshi',$keshi);
        $this->assign('renqun',$renqun);
        $this->assign('list', $list['rows']);
        $this->assign('page',$list['pageHtml']);
        $this->display();
    }


    /*  * 新增
     * @author jiaolele
     */
    public function add(){
        $diseaseModel = new DiseaseModel();
        if(IS_POST){
            $params = I('post.');
            $params['summary']= base64_encode(I('post.summary'));
            $params['disease_reason']=base64_encode( I('post.disease_reason'));
            $params['clinical_manifestations']= base64_encode(I('post.clinical_manifestations'));
            $params['complication']=base64_encode(I('post.complication')) ;
            $params['laboratory_check']= base64_encode(I('post.laboratory_check'));
            $params['diagnosis']= base64_encode(I('post.diagnosis'));
            $params['treatment']=base64_encode(I('post.treatment'));
            $params['prophylaxis']=base64_encode(I('post.prophylaxis'));
            $params['backup']=base64_encode(I('post.backup'));
            $rst = $diseaseModel->addDisease($params);
            if($rst===false){
                $this->ajaxReturn(array('code'=>1,'msg'=>$diseaseModel->getError()));
            }else{
                $this->ajaxReturn(array('code'=>0,'url'=>U('index',array('key'=>'DISEASELIST'))));
            }
        }else{
            $keshi = M('DiseaseCategary')->field('id,type_name')->where(array('category'=>0))->select();//查询所有科室分类
            $renqun= M('DiseaseCategary')->field('id,type_name')->where(array('category'=>1))->select();//查询所有人群分类
            $this->assign('keshi',$keshi);
            $this->assign('renqun',$renqun);
            $this->display();
        }
    }

    /**
     * 编辑数据
     * @param $id
     * @author jiaolele
     */
    public function edit($id){
        $diseaseModel = new DiseaseModel();
        if(IS_POST){
            $params = I('post.');
            $params['summary']= base64_encode(I('post.summary'));
            $params['disease_reason']=base64_encode( I('post.disease_reason'));
            $params['clinical_manifestations']= base64_encode(I('post.clinical_manifestations'));
            $params['complication']=base64_encode(I('post.complication')) ;
            $params['laboratory_check']= base64_encode(I('post.laboratory_check'));
            $params['diagnosis']= base64_encode(I('post.diagnosis'));
            $params['treatment']=base64_encode(I('post.treatment'));
            $params['prophylaxis']=base64_encode(I('post.prophylaxis'));
            $params['backup']=base64_encode(I('post.backup'));
            $rst = $diseaseModel->saveDisease($params);
            if($rst===false){
                $this->ajaxReturn(array('code'=>1,'msg'=>$diseaseModel->getError()));
            }else{
                $this->ajaxReturn(array('code'=>0,'url'=>Cookie('__forward__')));
            }
        }else{
            //得到单个信息
            $info = $diseaseModel->find($id);
            $case['id']=$info['id'];
            $case['section_office_id']=$info['section_office_id'];
            $case['crowd_id']=$info['crowd_id'];
            $case['disease_name']=$info['disease_name'];
            $case['summary']=base64_decode($info['summary']);
            $case['disease_reason']=base64_decode($info['disease_reason']);
            $case['clinical_manifestations']=base64_decode($info['clinical_manifestations']);
            $case['complication']=base64_decode($info['complication']);
            $case['laboratory_check']=base64_decode($info['laboratory_check']);
            $case['diagnosis']=base64_decode($info['diagnosis']);
            $case['treatment']=base64_decode($info['treatment']);
            $case['prophylaxis']=base64_decode($info['prophylaxis']);
            $case['backup']=base64_decode($info['backup']);
            $case['sort']=$info['sort'];
            $keshi = M('DiseaseCategary')->field('id,type_name')->where(array('category'=>0))->select();//查询所有科室分类
            $renqun= M('DiseaseCategary')->field('id,type_name')->where(array('category'=>1))->select();//查询所有人群分类
            $this->assign('keshi',$keshi);
            $this->assign('renqun',$renqun);
            $this->assign($case);
            $this->display('add');
        }
    }


    /**
     * 删除数据
     * @author jiaolele
     */
    public function remove($id,$file = '')
    {
        $diseaseModel = new DiseaseModel();
        $rst = $diseaseModel->remove($id,$file);
        if ($rst !== false) {
            $this->ajaxReturn(array('code' => 0));
        } else {
            $this->ajaxReturn(array('code' => 1, 'msg' => $diseaseModel->getError()));
        }
    }
}